package com.hong.taotaomall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hong.taotaomall.entity.Result;
import com.hong.taotaomall.entity.Shops;
import com.hong.taotaomall.service.IShopsService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 商家信息 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/shops")
public class ShopsController {
    @Autowired
    private IShopsService shopsService;

    @PostMapping("/login")
    public Result<Shops> login(@RequestBody Shops shops, HttpSession session){
        String password = shops.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Shops> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Shops::getUsername,shops.getUsername());
        Shops shops1 = shopsService.getOne(lqw);
        if(shops1 ==null){
            return Result.error("登陆失败，用户不存在");
        }
        if(!shops1.getPassword().equals(password)){
            return Result.error("登陆失败，密码错误");
        }
        if(shops1.getStatus() == 0){
            return Result.error("账号已禁用");
        }
        session.setAttribute("shops",shops1.getId());
        return Result.success(shops1);
    }

    @PostMapping("/logout")
    public Result<String > logout( HttpSession session){
        session.removeAttribute("shops");
        return Result.success("退出成功");
    }

    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name){
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Shops> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Shops::getName,name);
        queryWrapper.orderByDesc(Shops::getUpdateTime);
        shopsService.page(pageInfo,queryWrapper);
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result<String> save(HttpSession session,@RequestBody Shops shops){
        shops.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        shopsService.save(shops);
        return Result.success("商家新增成功");
    }

    @PutMapping
    public Result<String> update(HttpServletRequest request, @RequestBody Shops shops){
//        log.info("编辑商家信息");
        Long Id = (Long)request.getSession().getAttribute("shops");
        shopsService.updateById(shops);
        return Result.success("商家信息修改成功");
    }

    @GetMapping("/{id}")
    public Result<Shops> updateEmp(@PathVariable Long id){
//        log.info("获取id");
        Shops shops = shopsService.getById(id);
        if(shops != null){
            return Result.success(shops);

        }
        return Result.error("商家信息修改失败");
    }

}
